package com.yanze.schoolinformation.adapter.rv;

import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;

import com.yanze.schoolinformation.R;
import com.yanze.schoolinformation.base.baseadapter.BaseRecyclerViewAdapter;
import com.yanze.schoolinformation.base.baseadapter.BaseRecyclerViewHolder;
import com.yanze.schoolinformation.databinding.ItemCustomBinding;
import com.yanze.schoolinformation.helper.ImageHelper;
import com.yanze.schoolinformation.model.bean.GankDataBean;
import com.yanze.schoolinformation.view.webview.WebViewActivity;

/**
 * Created by MrZheng on 2018/4/27.
 */

public class GankAdapter extends BaseRecyclerViewAdapter<GankDataBean.ResultsBean> {

    private boolean isAll = false;
    private Context context;

    public GankAdapter(Context context) {
        this.context = context;
    }

    public void setAllType(boolean all) {
        isAll = all;
    }

    @NonNull
    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(parent, R.layout.item_custom);
    }

    private class ViewHolder extends BaseRecyclerViewHolder<GankDataBean.ResultsBean, ItemCustomBinding> {
        public ViewHolder(ViewGroup viewGroup, int layoutId) {
            super(viewGroup, layoutId);
        }

        @Override
        public void onBindViewHolder(GankDataBean.ResultsBean gankBean, int position) {
            if (isAll && "福利".equals(gankBean.getType())) {
                binding.ivAllWelfare.setVisibility(View.VISIBLE);
                binding.llWelfareOther.setVisibility(View.GONE);
                ImageHelper.displayEspImage(gankBean.getUrl(), binding.ivAllWelfare, 1);
            } else {
                binding.ivAllWelfare.setVisibility(View.GONE);
                binding.llWelfareOther.setVisibility(View.VISIBLE);
            }

            if (isAll) {
                binding.tvContentType.setVisibility(View.VISIBLE);
                binding.tvContentType.setText(" · " + gankBean.getType());
            } else {
                binding.tvContentType.setVisibility(View.GONE);

            }

            // 显示gif图片会很耗内存
            if (gankBean.getImages() != null
                    && gankBean.getImages().size() > 0
                    && !TextUtils.isEmpty(gankBean.getImages().get(0))) {
                binding.ivAndroidPic.setVisibility(View.VISIBLE);
                ImageHelper.displayGif(gankBean.getImages().get(0), binding.ivAndroidPic);
            } else {
                binding.ivAndroidPic.setVisibility(View.GONE);
            }

            binding.setResultsBean(gankBean);
            binding.setCommand(GankAdapter.this);
            binding.executePendingBindings();
        }
    }

    public void openDetail(GankDataBean.ResultsBean object) {
        WebViewActivity.loadUrl(context, object.getUrl(), object.getDesc());
    }
}
